package com.speechb.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.speechb.common.text.PageUtil;
import com.speechb.controller.base.BaseController;
import com.speechb.entity.Blog;
import com.speechb.service.IBlogService;
import com.speechb.service.IMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : llj
 * @version :2.1.0
 * @date : 2020/6/14 9:33
 */
@Controller
public class WebUrlController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WebUrlController.class);

    @Autowired
    IBlogService blogService;

    @Autowired
    IMenuService menuService;

    private static final int PAGE_SIZE = 12;

    @GetMapping(value = {"/", ""})
    public String index(Model model) {

        Map<String, Object> map = new HashMap<>(2);
        Page pageHelper = PageHelper.startPage(1, PAGE_SIZE, true);
        List<Blog> blogList = blogService.findAll(null);
        blogService.convertConverUrl(blogList);
        map.put("index", PageUtil.getPageInd(1, null, pageHelper.getPages(), "/list/"));
        map.put("list", blogList);
        map.put("menus", menuService.menulist());
        model.addAllAttributes(map);
        return "web/app/index";
    }

    @GetMapping("/list/{id}/{pageNum}")
    public ModelAndView list(@PathVariable Integer id, @PathVariable Integer pageNum) {
        Page pageHelper = PageHelper.startPage(pageNum, PAGE_SIZE, true);
        List<Blog> blogList = blogService.findAll(id);
        blogService.convertConverUrl(blogList);
        ModelAndView mav = new ModelAndView("web/app/index");
        mav.addObject("list", blogList);
        mav.addObject("index", PageUtil.getPageInd(pageNum, id, pageHelper.getPages(), "/list/"));
        mav.addObject("menus", menuService.menulist());
        return mav;
    }

    @GetMapping("/list/{pageNum}")
    public ModelAndView list(@PathVariable Integer pageNum) {
        Page pageHelper = PageHelper.startPage(pageNum, PAGE_SIZE, true);
        List<Blog> blogList = blogService.findAll(null);
        blogService.convertConverUrl(blogList);
        ModelAndView mav = new ModelAndView("web/app/index");
        mav.addObject("list", blogList);
        mav.addObject("index", PageUtil.getPageInd(pageNum, null, pageHelper.getPages(), "/list/"));
        mav.addObject("menus", menuService.menulist());
        return mav;
    }

    @GetMapping("/login")
    public String login() {
        return "web/app/login";
    }
    @GetMapping("/register")
    public String register() {
        return "web/app/register";
    }
    @GetMapping("/error/404")
    public String error404() {
        return "web/app/error/404";
    }
    @GetMapping("/error/500")
    public String error500() {
        return "web/app/error/500";
    }

    @GetMapping("/json")
    public String json() {
        return "web/app/json";
    }

    @GetMapping("/encode")
    public String encode() {
        return "web/app/encode";
    }

    @GetMapping("/publish")
    public String publish() {
        return "web/app/mk_publish";
    }

}
